# -*- encoding: utf-8 -*-
'''
@文件    :bfslayer.py
@说明    :给你一个二叉树，请你返回其按 层序遍历 得到的节点值。 （即逐层地，从左到右访问所有节点）。
示例：
二叉树：[3,9,20,null,null,15,7]
[
  [3],
  [9,20],
  [15,7]
]
@时间    :2020/06/05 16:07:49
@作者    :张钧铭
'''

# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """

        queue = p = [root]
        
        ret = []
        res = []
        i = 0

        while queue:
            for i in range(len(queue)):
                p=(queue.pop(0))
                if p.left:
                    queue.append(p.left)
                if p.right:
                    queue.append(p.right)
                ret.append(p.val)
            res.append(ret)
            ret=[]

        return res

def stringToTreeNode(input):
    input = input.strip()
    input = input[1:-1]
    if not input:
        return None

    inputValues = [s.strip() for s in input.split(',')]
    root = TreeNode(int(inputValues[0]))
    nodeQueue = [root]
    front = 0
    index = 1
    while index < len(inputValues):
        node = nodeQueue[front]
        front = front + 1

        item = inputValues[index]
        index = index + 1
        if item != "null":
            leftNumber = int(item)
            node.left = TreeNode(leftNumber)
            nodeQueue.append(node.left)

        if index >= len(inputValues):
            break

        item = inputValues[index]
        index = index + 1
        if item != "null":
            rightNumber = int(item)
            node.right = TreeNode(rightNumber)
            nodeQueue.append(node.right)
    return root

def int2dArrayToString(input):
    return json.dumps(input)

def main():
    import sys
    # def readlines():
    #     testinput = '[3,9,20,null,null,15,7]'
    #     for line in testinput:
    #         yield line.strip('\n')
    # lines = readlines()
    while True:
        try:
            line = '[3,9,20,null,null,15,7]'
            root = stringToTreeNode(line)
            
            ret = Solution().levelOrder(root)

            out = int2dArrayToString(ret)
            print(out)
        except StopIteration:
            break

if __name__ == '__main__':
    main()